package com.taobao.tql.parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.taobao.tql.bean.ApiDetailBean;
import com.taobao.tql.bean.ApiObjectBean;
import com.taobao.tql.scan.GetApiImpl;

public class JsonParse {
	
	/**
	 * 
	 * isMustInput(这里用一句话描述这个方法的作用)
	 * 判断接口属性是否必输
	 * @param treeMap
	 * @param apiId
	 * @param apiName
	 * @return
	 * String
	 * @since 1.0.0
	 */
	public static String isMustInput(Map<String, String> treeMap, String cid, String apiName) {
		String renValue = null;
		List<ApiDetailBean> getApiProps = getApiProps( cid,  apiName);
		for(ApiDetailBean tempBean : getApiProps) {
			String name = treeMap.get(tempBean.getName());
			if(!(tempBean.getName().equals("fields")) 
					&& tempBean.getClassname().equals("isMust") 
					&& name == null) {
				renValue = tempBean.getName() + " is must input!!!";
				break;
				
			}
		}
		return renValue;
	}


	/**
	 * 
	 * getApiDetailStr(这里用一句话描述这个方法的作用)
	 * 获取接口明细
	 * @param apiId
	 * @param apiName
	 * @return
	 * String
	 * @since 1.0.0
	 */
	public static List<ApiDetailBean> getApiDetailStr(String apiId, String apiName) {
		HttpClient client = new HttpClient();
		// 设置代理服务器地址和端口
		// client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
		// 使用 GET 方法 ，如果服务器需要通过 HTTPS 连接，那只需要将下面 URL 中的 http 换成 https
		HttpMethod method = new GetMethod("http://my.open.taobao.com//apitools/getApiParamList.htm?apiId=" + apiId +"&apiName=" + apiName);
		HttpMethodParams getParams = new HttpMethodParams();
		getParams.setParameter("cid", "1");

		// NameValuePair pass = new NameValuePair( "password" , "ld" );
		//method.setParams(getParams);
		// 使用POST方法
		// HttpMethod method = new PostMethod("http://java.sun.com");
		try {
			client.executeMethod(method);
		} catch (HttpException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String respondJsonStr = null;
		try {
			respondJsonStr = method.getResponseBodyAsString();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// 打印服务器返回的状态
		System.out.println(method.getStatusLine());
		// 打印返回的信息
		System.out.println(respondJsonStr);
		// 释放连接
		method.releaseConnection();
		List<ApiDetailBean> listApiDetails = null;
		try {
			listApiDetails = validateDetail(respondJsonStr);

			//jo.tob(tempArr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listApiDetails;
	}


	/**
	 * 
	 * getHTTPStr(这里用一句话描述这个方法的作用)
	 * 获取接口属性
	 * @return
	 * String
	 * @since 1.0.0
	 */
	public static Map<String, ApiObjectBean> getAllApis(String cid) {
		HttpClient client = new HttpClient();
		// 设置代理服务器地址和端口
		// client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
		// 使用 GET 方法 ，如果服务器需要通过 HTTPS 连接，那只需要将下面 URL 中的 http 换成 https
		HttpMethod method = new GetMethod("http://my.open.taobao.com/apitools/getApiList.htm?cid=" + cid);
		try {
			client.executeMethod(method);
		} catch (HttpException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String respondJsonStr = null;
		try {
			respondJsonStr = method.getResponseBodyAsString();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// 打印服务器返回的状态
		System.out.println(method.getStatusLine());
		// 打印返回的信息
		System.out.println(respondJsonStr);
		// 释放连接
		method.releaseConnection();
		Map<String, ApiObjectBean> apiMap = null;
		try {
			 apiMap = validateMore(respondJsonStr);
			//jo.tob(tempArr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return apiMap;
	}
	
	
	/**
	 * 
	 * getHTTPStr(这里用一句话描述这个方法的作用)
	 * 获取接口属性
	 * @return
	 * String
	 * @since 1.0.0
	 */
	public static List<ApiDetailBean> getApiProps(String cid, String apiName) {

		List<ApiDetailBean> listDetails = null;
		try {
			Map<String, ApiObjectBean> apiMap = GetApiImpl.getInstantce().getApiMaps();
			ApiObjectBean tempBean = apiMap.get(apiName);
			listDetails = getApiDetailStr(tempBean.getApiId(), tempBean.getApiName());
			//jo.tob(tempArr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listDetails;
	}

	/**
	 * 校验多个EtShopFlightSetupDO对象的json格式的数组，返回EtShopFlightSetupDO的list
	 * [{"name":"nick","type":"text","classname":"isMust","desc":"表示卖家昵称","demoValue":"zhangsan"}]
	 * @param jsonFormParas 形如[{},{},...,{}]
	 * @return
	 */
	public static List<ApiDetailBean> validateDetail(String jsonFormParas) {
		List<ApiDetailBean> fsList = new ArrayList<ApiDetailBean>();

		JSONArray jsonObject = null;
		try {
			jsonObject = new JSONArray(jsonFormParas);

			for (int i = 0; i < jsonObject.length(); i++) {
				if(!jsonObject.isNull(i)) {
					JSONObject jsonTemp = new JSONObject(jsonObject.get(i).toString());
					ApiDetailBean fs = new ApiDetailBean();
					fs.setClassname(String.valueOf(jsonTemp.getString("classname")));
					fs.setName(String.valueOf(jsonTemp.getString("name")));
					fs.setDemoValue(String.valueOf(jsonTemp.getString("demoValue")));
					fs.setDesc(String.valueOf(jsonTemp.getString("desc")));
					fs.setType(String.valueOf(jsonTemp.getString("type")));
					System.out.println(fs.toString());
					
					fsList.add(fs);
				}
			}
		} catch (JSONException e) {
			throw new IllegalArgumentException(e);
		}
		return fsList;
	}
	
	

	/**
	 * 校验多个EtShopFlightSetupDO对象的json格式的数组，返回EtShopFlightSetupDO的list
	 * @param jsonFormParas 形如[{},{},...,{}]
	 * @return
	 */
	public static Map<String, ApiObjectBean> validateMore(String jsonFormParas) {
		Map<String, ApiObjectBean> fsList = new TreeMap<String, ApiObjectBean>();

		JSONArray jsonObject = null;
		try {
			jsonObject = new JSONArray(jsonFormParas);

			for (int i = 0; i < jsonObject.length(); i++) {
				if(!jsonObject.isNull(i)) {
					JSONObject jsonTemp = new JSONObject(jsonObject.get(i).toString());
					ApiObjectBean fs = new ApiObjectBean(String.valueOf(jsonTemp.getString("apiName")),  String.valueOf(jsonTemp.getString("apiId")));
					System.out.println(fs.toString());
					
					fsList.put(fs.getApiName(), fs);
				}
			}
		} catch (JSONException e) {
			throw new IllegalArgumentException(e);
		}
		return fsList;
	}
}
